<template>
	<u-modal v-model="show" :show-cancel-button="update_type!=3 ? true : false" confirm-text="升级" :title="update_title"
	 @cancel="cancel" @confirm="confirm">
		<view class="u-update-content">
			<rich-text :nodes="update_content"></rich-text>
		</view>
		<u-toast ref="uToast" />
	</u-modal>
</template>

<script>
	export default {
		data() {
			return {
				show: true,
				update_type: 1,
				update_title: '发现新版本',
				update_content: '',
				update_url: ''
			}
		},
		onLoad(option) {
			this.update_type = option.update_type;
			this.update_title = option.update_title;
			this.update_content = option.update_content;
			this.update_url = option.update_url;
			this.show = true;
		},
		methods: {
			cancel() {
				uni.reLaunch({
					url:'/pages/home/index'
				})
			},
			confirm() {
				// #ifdef APP-PLUS  
				const _self = this;
				var dtask = plus.downloader.createDownload(this.update_url, {}, function(d, status) {
					// 下载完成  
					if (status == 200) {
						plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, function(error) {
							_self.$refs.uToast.show({
								title: '安装失败',
								type: 'error',
								position: 'top'
							})
						})
					} else {
						_self.$refs.uToast.show({
							title: '更新失败',
							type: 'error',
							position: 'top'
						})
					}
				});
				this.$refs.uToast.show({
					title: '开始下载',
					type: 'success',
					position: 'top',
					isTab: true,
					url: '/pages/index/index',
					callback: () => {
						dtask.start();
					}
				})
				// #endif
			},
			closeModal() {
				uni.switchTab({
					url: '/pages/index/index'
				});
			}
		}
	}
</script>

<style scoped>
	.u-full-content {
		background-color: #00C777;
	}

	.u-update-content {
		font-size: 26rpx;
		color: $u-content-color;
		line-height: 1.7;
		padding: 30rpx;
	}
</style>
